package hot150;

import java.util.Arrays;

// https://leetcode.cn/problems/jump-game-ii/description/?envType=study-plan-v2&envId=top-interview-150
public class q10 {

    public int jump(int[] nums) {
        return method1(nums, nums.length);
    }

    /**
     * 贪心
     * 1 ms
     * 复杂度O(n)
     */
    public int method1(int[] nums, int n) {
        int count = 0, step = 0, mx = 0;
        for (int i = 0 ; i < n && step < n - 1 ; i++) {
            // 获取可以达到的最远距离
            mx = Math.max(mx, i + nums[i]);
            // 上一步（最少的步数）可以到达的最远距离
            if (step == i) {
                count++;
                step = mx;
            }
        }
        return count;
    }
}
